//
//  SplashScene.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 8/17/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "SplashScene.h"
#import "MenuScene.h"
#import "SimpleAudioEngine.h"
#import "Setting.h"
#import "MovieScene.h"

@implementation SplashScene

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    SplashScene *layer = [SplashScene node];
    [scene addChild:layer];
    return scene;
}

-(id) init
{
    if (self = [super init])
    {
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:PLIST_INGAME textureFilename:IMAGE_INGAME];
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:PLIST_UI textureFilename:IMAGE_UI];
        
        [[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:SOUND_BG];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_ENERGY_COLLECT];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_BOMB_EXPLOSION];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_LAZER];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_MENU_OPEN];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_MENU_BUTTON];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_SECOND];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_STAR];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_SHIELD];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_SUN];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_TIME];
        [[SimpleAudioEngine sharedEngine] preloadEffect:SOUND_MAGNET];
        
        CCDelayTime *delay = [CCDelayTime actionWithDuration:2.5];
        CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            if ([defaults objectForKey:KEY_FIRST_TIME_OPEN] == nil)
            {
                [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[MovieScene scene]]];
            }
            else
            {
                [[CCDirector sharedDirector] replaceScene:[CCTransitionFade transitionWithDuration:0.5 scene:[MenuScene scene]]];
            }
            if ([Setting getInstance]._isSoundBG)
                [[SimpleAudioEngine sharedEngine] playBackgroundMusic:SOUND_BG];
        }];
        CCSequence *sequence = [CCSequence actions:delay, callBlock, nil];
        [self runAction:sequence];
        
        
        int mListPixels[5][23] =
        {
            {1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,},
            {1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1,},
            {1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1,},
            {1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1,},
            {1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1,},
        };
    
        
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        int blockSize = uniPos(6, 12);
        int paddleSize = uniPos(1, 2);
        int spaceSize = uniPos(6, 12);
        float x;
        float y;
        
        int dash[23] = {paddleSize, paddleSize, paddleSize, spaceSize, spaceSize, paddleSize, paddleSize, spaceSize, paddleSize, paddleSize, spaceSize, paddleSize, paddleSize, spaceSize, paddleSize, paddleSize, spaceSize, spaceSize, paddleSize, paddleSize, spaceSize, paddleSize, paddleSize};
        
        int totalX = blockSize * 23;
        int totalY = blockSize * 5 + 4 * paddleSize;
        
        for (int i = 0; i < 23; i++)
        {
            totalX += dash[i];
        }
        
        x = (winSize.width - totalX) / 2;
        y = (winSize.height + totalY) / 2;
        
        rtx = [CCRenderTexture renderTextureWithWidth:blockSize height:blockSize];
        [rtx beginWithClear:0.0 g:0.0 b:0.0 a:0.0];
        [rtx end];
        [rtx retain];
        
        circle = [CCSprite spriteWithSpriteFrameName:SFN_PNODE];
        [circle setScale:0];
        [circle retain];
        [circle setPosition:ccp(blockSize / 2, blockSize / 2)];
        
        for (int i = 0; i < 5; i++)
        {
            y -= paddleSize + blockSize;
            x = (winSize.width - totalX) / 2;
            for (int j = 0; j < 23; j++)
            {
                x += dash[j];
                if (mListPixels[i][j] == 1)
                {
                    CCSprite *sprite = [CCSprite spriteWithTexture:rtx.sprite.texture];
                    [sprite setPosition:ccp(x, y)];
                    [self addChild:sprite];
                }
                x += blockSize;
            }
        }
        
        [self scheduleUpdate];
 
    }
    
    return self;
}

-(void) update:(ccTime)delta
{
    if (circle.scale > 1)
        return;
    
    [circle setScale:circle.scale + 0.01];
    [rtx beginWithClear:0.0 g:0.0 b:0.0 a:0.0];
    [circle visit];
    [rtx end];
}

-(void) dealloc
{
    [circle release];
    [rtx release];
    [super dealloc];
}

@end
